﻿using System;
using System.Windows;
using System.Windows.Input;

using TimedTask.Tools;
using TimedTask.Models;

namespace TimedTask
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Modules.TaskListModule _tlModule = new Modules.TaskListModule();  //任务列表模块
        private Modules.NoteListModule _nlModule = new Modules.NoteListModule();  //记事本列表模块
        public MainWindow()
        {
            InitializeComponent();
            XamlHelper.Instance.SetBackground(this.borderTop, Const.AppBgImg);

            this.btnMenu.Click += (s, e) =>
            {
                this.sysMenu.IsOpen = true;
            };

            #region 窗体事件

            this.MouseLeftButtonDown += (s, e) =>
            {
                if (e.LeftButton == MouseButtonState.Pressed) this.DragMove();
            };
            //窗体最小化
            this.btnMin.Click += (s, e) =>
            {
                this.WindowState = WindowState.Minimized;
                this.Visibility = Visibility.Hidden;//最小化到托盘
            };
            //窗体关闭
            this.btnClose.Click += (s, e) =>
            {
                if (Const.MinToTray)
                {
                    Minimized();
                    NotifyIconHelper.Instance().ShowBalloonTip("提示", "程序已经最小化到系统托盘！");
                    return;
                }
                Helper.ExitApp();
            };

            #endregion
            #region 主菜单

            //记事
            this.btnNote.Click += (s, e) =>
            {
                if (this.brMain.Child != this._nlModule) this.brMain.Child = this._nlModule;
            };
            //任务
            this.btnTask.Click += (s, e) =>
            {
                if (this.brMain.Child != this._tlModule) this.brMain.Child = this._tlModule;
            };

            #endregion

            if (this.brMain.Child != this._tlModule) {
                this.brMain.Child = this._tlModule;
            }
            NotifyIconHelper.NotifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(TaskBarLeftDown_Click);
        }

        #region 托盘相关

        /// <summary>
        /// 任务栏单击击图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TaskBarLeftDown_Click(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.Show();
                if (this.WindowState == WindowState.Minimized)
                {
                    this.WindowState = WindowState.Normal;
                }
                this.Activate();
            }
        }
        #endregion

        #region 窗体

        /// <summary>
        /// 窗口最小化
        /// </summary>
        private void Minimized()
        {
            this.WindowState = WindowState.Minimized;
            this.Visibility = Visibility.Hidden;//最小化到托盘
        }

        #endregion

        #region 菜单事件
        //下拉菜单
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            string arg = ((sender as System.Windows.Controls.MenuItem).CommandParameter).ToString();
            if (arg.Length == 0)
                return;

            switch (arg)
            {
                case "1"://启动宠物
                    Helper.Alert("开发中，敬请期待！");
                    break;
                case "upgrade"://检查更新
                    Helper.Alert("开发中，敬请期待！");
                    break;
                //case "news"://新闻资讯
                //    Views.NewsList news = new Views.NewsList();
                //    news.ShowDialog();
                //    break;
                case "about"://关于
                    Views.About about = new Views.About();
                    about.Show();
                    break;
                case "exit"://退出
                    System.Windows.Application.Current.Shutdown();
                    //Helper.EndApp("img.exe");
                    break;
                case "set"://设置
                    Views.Config config = new Views.Config();
                    config.ShowDialog(); ;
                    break;
                case "tools-down"://网页图片下载
                    Views.PageFileDown fileDown = new Views.PageFileDown();
                    fileDown.Show();
                    break;
                case "drive-cd"://虚拟光驱
                    Helper.StartApp(Const.StartPath + "\\Tools\\virtualdrivemaster.exe");
                    break;
                case "tools-img"://图片压缩
                    this.sysMenu.IsOpen = false;
                    Helper.StartApp(Const.StartPath + "\\Tools\\img.exe");
                    break;
                case "tools-reg"://正则工具
                    this.sysMenu.IsOpen = false;
                    Helper.StartApp(Const.StartPath + "\\Tools\\reg.exe");
                    break;
                default:
                    break;
            }
            if (this.brMain.Child != this._tlModule)
            {
                this.brMain.Child = this._tlModule;
            }
        }

        #endregion
    }
}
